package ar.com.photo_admin.service

import javax.annotation.PostConstruct

import org.codehaus.groovy.grails.commons.GrailsApplication
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.context.i18n.LocaleContextHolder as LCH

class BaseService implements ApplicationContextAware {

	ApplicationContext applicationContext
	GrailsApplication grailsApplication
	
	def springSecurityService
	def messageSource
	def g
	
	def sessionFactory
	
	def serverUrl
	def localPrefix
	def hostedPrefix		
	
	@PostConstruct
	def initBaseService(){
		localPrefix = grailsApplication.config.photos.path.notInHosting
		hostedPrefix = grailsApplication.config.photos.path.inHosting
		serverUrl = grailsApplication.config.grails.serverURL		
		g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
	}
	
	def getImagesRootPath(){
		return applicationContext.getResource("images/upload").getFile().toString() + File.separatorChar
	}
	
	def getOrdersRootPath(){
		return applicationContext.getResource("ordenes").getFile().toString() + File.separatorChar
	}
	
	def getEventNotHostedPhotosPath(def eventNumber){
		return getImagesRootPath() + eventNumber + File.separator + localPrefix + File.separator
	}
	
	def getEventHostedPhotosPath(def eventNumber){
		return getImagesRootPath() + eventNumber + File.separator + hostedPrefix + File.separator
	}
	
	def getEventNotHostedPhotosUrl(def eventNumber){
		return serverUrl + "/images/upload/" + eventNumber + "/" + localPrefix + "/"
	}
	
	def getEventHostedPhotosUrl(def eventNumber){
		return serverUrl + "/images/upload/" + eventNumber + "/" + hostedPrefix + "/"
	}
	
	def getLoggedUser(){
		springSecurityService.getCurrentUser()
	}
	
	def getMessage(code){
		return messageSource.getMessage(code, null, LCH.getLocale())
	}
}
